package com.itheima.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.swing.*;

//把SpringMvcConfig 配置文件加载到tomcat容器
//配置拦截路径-找到指定的controller映射路径
public class ServletlnitConfig extends AbstractAnnotationConfigDispatcherServletInitializer{
    //其他资源配置
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }
    //参与映射的资源配置
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
    //拦截的映射路径
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //接收post参数乱码
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter f = new CharacterEncodingFilter();
        f.setEncoding("utf-8");
        return new Filter[]{f};
    }
}
//public class ServletlnitConfig extends AbstractDispatcherServletInitializer {
//    //把SpringMvcConfig 配置文件加载到tomcat容器
//    @Override
//    protected WebApplicationContext createServletApplicationContext() {
//        AnnotationConfigWebApplicationContext app = new AnnotationConfigWebApplicationContext();
//        app.register(SpringMvcConfig.class);
//        return app;
//    }
//    //配置拦截路径-找到指定的controller映射路径
//    @Override
//    protected String[] getServletMappings() {
//        return new String[]{"/"};//拦截路径
//    }
//    //其他资源配置（这里的资源不参与映射匹配）
//    @Override
//    protected WebApplicationContext createRootApplicationContext() {
//        AnnotationConfigWebApplicationContext app = new AnnotationConfigWebApplicationContext();
//        app.register(SpringConfig.class);
//        return app;
//    }
//}
